<template>
  <div class="organize-table-container">
    <OrganizeTree
      ref="OrganizeTreeRef"
      @activeChange="changeOranize"
      :activeOranize="activeOranize"
      roleType="1"
    />
    <TableList ref="TableListRef" :activeOranize="activeOranize" />
  </div>
</template>

<script setup name="resources-sites">
import OrganizeTree from '@/components/OrganizeTree';
import TableList from './components/TableList';
import { searchParams } from './components/tableConst';
import userStore from '@/store/modules/user';
import systemStore from '@/store/modules/system';
const route = useRoute();

const activeOranize = ref({});
const TableListRef = ref();

const OrganizeTreeRef = ref();
onActivated(() => {
  if (!systemStore().resetSitesInfo) {
    OrganizeTreeRef.value.getData();
  }
  TableListRef.value.getOptions();
});
async function changeOranize(info) {
  const keep = activeOranize.value.id === info.id;
  activeOranize.value = info;
  await nextTick();
  TableListRef.value.initTable(keep);
}

watch(
  () => systemStore().resetSitesInfo,
  async val => {
    if (!val) return;
    systemStore().setResetSites(false);
    searchParams.is_due_soon = val.is_due_soon;
    searchParams.group_id = val.group_id;
    searchParams.user_ids = val.user_ids;
    if (searchParams.user_ids?.[0]) {
      searchParams.user_name =
        userStore().userMap[searchParams.user_ids?.[0]]?.label;
    }
    activeOranize.value.id = searchParams.group_id;
    await nextTick();
    OrganizeTreeRef.value.getData();
  },
  { immediate: true }
);
</script>
